import 'package:flutter/material.dart';

class LoadingButton extends StatelessWidget {
  String text;
  bool loading;
  VoidCallback onPressed;

  LoadingButton(this.text, this.loading, this.onPressed);

  @override
  Widget build(BuildContext context) {
    double height = 50;
    double width = 230;
    var textStyle = TextStyle(
        color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500);
    final textPainter = TextPainter(
      textDirection: TextDirection.ltr,
      text: TextSpan(
        text: text,
        style: textStyle,
      ),
    );
    textPainter.layout();
    var textWidth = textPainter.width;
    double progressWidth = loading ? height - 10 : 0;
    double contextWidth = progressWidth + textWidth;

    return GestureDetector(
      onTap:(){
        if(!loading){
          this.onPressed();
        }
      },
      child: Container(
        width: width,
        height: height,
        alignment: Alignment.center,
        decoration: ShapeDecoration(
          color: Colors.blue,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(8.0)),
          ),
        ),
        child: Container(
          width: contextWidth,
          alignment: Alignment.center,
          margin: EdgeInsets.only(right: progressWidth),
          child: Row(
            children: <Widget>[
              loading
                  ? Container(
                      width: progressWidth,
                      height: progressWidth,
                      padding: EdgeInsets.all(10),
                      child: CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation(Colors.orange),
                      ),
                    )
                  : Container(),
              Text(
                text,
                style: textStyle,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
